package main

import "fmt"

// -----抽象层------
//水果类（抽象接口）
type Fruit interface {
	Show()
}

//工厂类 （抽象接口）
type AbstractFactory interface {
	CreateFruit() Fruit //生产水果类（抽象）生产方法
}

//-----基础模块层-----
type Apple struct {
	Fruit
}

func (a *Apple) Show() {
	fmt.Println("我是苹果")
}

type Banana struct {
	Fruit
}

func (b *Banana) Show() {
	fmt.Println("我是香蕉")
}

type Pear struct {
	Fruit
}

func (p *Pear) Show() {
	fmt.Println("我是梨")
}

// -----基础工厂模块-----
// 具体的苹果工厂
type AppleFactory struct {
	AbstractFactory
}

func (afc *AppleFactory) CreateFruit() Fruit {
	var fruit Fruit
	//生产一个具体的苹果
	fruit = new(Apple)
	return fruit
}

//具体的香蕉工厂
type BananaFactory struct {
	AbstractFactory
}

func (bfc *BananaFactory) CreateFruit() Fruit {
	var fruit Fruit
	//生产一个具体的香蕉
	fruit = new(Banana)
	return fruit
}

//具体的梨工厂
type PearFactory struct {
	AbstractFactory
}

func (pfc *PearFactory) CreateFruit() Fruit {
	var fruit Fruit
	//生产一个具体的梨
	fruit = new(Pear)
	return fruit
}

// ----业务逻辑层----
func main() {
	//需求1：需要一个具体的苹果对象
	//1-先要一个具体的工厂
	var applefac AbstractFactory
	applefac = new(AppleFactory)

	var apple Fruit
	apple = applefac.CreateFruit()
	apple.Show()

	//需求2：需要一个具体的香蕉对象
	var bananafac AbstractFactory
	bananafac = new(BananaFactory)

	var banana Fruit
	banana = bananafac.CreateFruit()
	banana.Show()
}
